1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.devaki.nextobjects.workspace.models.graphics;
16 import java.io.Serializable;
17 import java.awt.Cursor;
18 import java.awt.Dimension;
19 import java.awt.Graphics2D;
20 import java.awt.Color;
21 import java.awt.Point;
22 import java.awt.Polygon;
23 import java.awt.event.MouseEvent;
24 import java.awt.event.MouseMotionAdapter;
25 import java.util.Vector;
26 import javax.swing.event.MouseInputAdapter;
27 import org.devaki.nextobjects.constants.CstGraphics;
28 import org.devaki.nextobjects.ui.menus.MenuFactory;
29 import org.devaki.nextobjects.ui.toolbars.NOToolBar2;
30 import org.devaki.nextobjects.ui.workspace.models.EditorFactory;
31 import org.devaki.nextobjects.util.ModelMan;
32 import org.devaki.nextobjects.util.NOGraphicsUtil;
33 import org.devaki.nextobjects.workspace.models.BaseModel;
34 import org.devaki.nextobjects.workspace.models.ConceptualModel;
35 import org.devaki.nextobjects.workspace.models.objects.Association;
36 import org.devaki.nextobjects.workspace.models.objects.AssociationLink;
37 import org.devaki.nextobjects.workspace.models.objects.BaseObject;
38 import org.devaki.nextobjects.workspace.models.objects.BaseClass;
39 import org.devaki.nextobjects.workspace.models.objects.Entity;
40 import org.devaki.nextobjects.workspace.models.objects.InheritanceLink;
41 /***
42 * This class is responsible for drawing a <code>ConceptualModel</code>
43 *
44 * @author <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
45 * @created December 22, 2003
46 * @see http://www.devaki.org/cdm.html
47 */
48 public class ConceptualView extends BaseModelView implements Serializable
49 {
50 /***
51 * Construct a new <code>ConceptualView</code> object
52 *
53 * @param pMerise the context Conceptual Data Model
54 */
55 public ConceptualView(final ConceptualModel pMerise)
56 {
57 super(pMerise);
58
59 this.drawingArea.addMouseListener(new MyMouseListener());
60 this.drawingArea.addMouseMotionListener(
61 new MyMouseMotionListener());
62 }
63 /***
64 * Calculate the visible objects
65 *
66 * @return an array containing the object views that are visible
67 */
68 public final ObjectView[] getVisibleObjectView()
69 {
70 ObjectView[] b =
71 new ObjectView[((ConceptualModel) myModel)
72 .getInheritanceLinks()
73 .size()
74 + ((ConceptualModel) myModel).getAssociationLinks().size()
75 + ((ConceptualModel) myModel).getEntities().size()
76 + ((ConceptualModel) myModel).getAssociations().size()
77 + ((ConceptualModel) myModel).getLabels().size()];
78 int offset = 0;
79 for (int i = 0;
80 i < ((ConceptualModel) myModel).getAssociationLinks().size();
81 i++)
82 {
83 b[offset] =
84 ((ConceptualModel) myModel)
85 .getAssociationLinkAt(i)
86 .getObjectView();
87 offset++;
88 }
89 for (int i = 0;
90 i < ((ConceptualModel) myModel).getInheritanceLinks().size();
91 i++)
92 {
93 b[offset] =
94 ((ConceptualModel) myModel)
95 .getInheritanceLinkAt(i)
96 .getObjectView();
97 offset++;
98 }
99 for (int i = 0;
100 i < ((ConceptualModel) myModel).getAssociations().size();
101 i++)
102 {
103 b[offset] =
104 ((ConceptualModel) myModel)
105 .getAssociationAt(i)
106 .getObjectView();
107 offset++;
108 }
109 for (int i = 0;
110 i < ((ConceptualModel) myModel).getEntities().size();
111 i++)
112 {
113 b[offset] =
114 ((ConceptualModel) myModel).getEntityAt(i).getObjectView();
115 offset++;
116 }
117 for (int i = 0;
118 i < ((ConceptualModel) myModel).getLabels().size();
119 i++)
120 {
121 b[offset] =
122 (ObjectView) ((ConceptualModel) myModel)
123 .getLabels()
124 .elementAt(
125 i);
126 offset++;
127 }
128 return b;
129 }
130 /*** Manage the drawing of links (Drag'n Drop operations) */
131 public final void manageDnD()
132 {
133 AssociationLink tmpLink;
134
135 if (ModelMan.getDraggedObject() instanceof Entity)
136 {
137
138 if (ModelMan.getDropTargetObject() instanceof Entity)
139 {
140 if (NOToolBar2.getCurrentTool()
141 == NOToolBar2.TOOL_ASSOCIATION_LINK)
142 {
143
144 Point tmpPoint =
145 NOGraphicsUtil.getMiddle(
146 ModelMan
147 .getDropTargetObject()
148 .getObjectView()
149 .getLocation(),
150 ModelMan
151 .getDraggedObject()
152 .getObjectView()
153 .getLocation());
154 this.x1 = (int) tmpPoint.getX();
155 this.y1 = (int) tmpPoint.getY();
156
157
158 Association newAssociation =
159 ModelMan.addAssociation(
160 (ConceptualModel) this.myModel,
161 tmpPoint);
162
163 tmpLink =
164 newAssociation.addAssociationLink(
165 (Entity) ModelMan.getDraggedObject());
166 ((LineView) tmpLink.getObjectView())
167 .computeBestPoints();
168 tmpLink =
169 newAssociation.addAssociationLink(
170 (Entity) ModelMan.getDropTargetObject());
171 ((LineView) tmpLink.getObjectView())
172 .computeBestPoints();
173
174 ModelMan.setCurrentObject(newAssociation);
175 }
176 else if (
177 NOToolBar2.getCurrentTool()
178 == NOToolBar2.TOOL_INHERITANCE)
179 {
180 InheritanceLink newInheritanceLink =
181 new InheritanceLink(
182 this.myModel,
183 (BaseClass) ModelMan.getDraggedObject(),
184 (BaseClass) ModelMan.getDropTargetObject());
185 ModelMan.addInheritanceLink(
186 (BaseModel) myModel,
187 newInheritanceLink);
188 }
189 }
190
191 else
192 {
193
194
195 tmpLink =
196 (
197 (Association) ModelMan
198 .getDropTargetObject())
199 .addAssociationLink(
200 (Entity) ModelMan.getDraggedObject());
201 ((LineView) tmpLink.getObjectView()).computeBestPoints();
202 }
203 }
204
205
206 else if (
207 ModelMan.getDraggedObject() instanceof Association
208 && ModelMan.getDropTargetObject() instanceof Entity)
209 {
210 tmpLink =
211 (
212 (Association) ModelMan
213 .getDraggedObject())
214 .addAssociationLink(
215 (Entity) ModelMan.getDropTargetObject());
216 ((LineView) tmpLink.getObjectView()).computeBestPoints();
217 }
218
219 ModelMan.setDraggedObject(null);
220 ModelMan.setDropTargetObject(null);
221
222 drawingArea.repaint();
223 }
224 /***
225 * MyMouseListener the class which handles mouse clicks on the conceptual
226 * model
227 *
228 * @author eflorent
229 * @created December 22, 2003
230 * @author: eflorent@devaki.org
231 */
232 class MyMouseListener extends MouseInputAdapter
233 {
234 /***
235 * What to do when the mouse is pressed
236 *
237 * @param e the mouse event to handle
238 */
239 public final void mousePressed(final MouseEvent e)
240 {
241 this.evaluatePopup(e);
242
243 x1 = e.getX();
244 y1 = e.getY();
245 if ((NOToolBar2.getCurrentTool() != NOToolBar2.TOOL_RESIZE)
246 && (NOToolBar2.getCurrentTool() != NOToolBar2.TOOL_TABLE)
247 && (NOToolBar2.getCurrentTool()
248 != NOToolBar2.TOOL_ASSOCIATION))
249 {
250 ObjectView[] tmp = getVisibleObjectView();
251 int j = 0;
252
253 boolean gotIt = false;
254 while (j < tmp.length)
255 {
256 if (((Polygon) tmp[j].getSurface()).contains(x1, y1))
257 {
258 gotIt = true;
259
260 dx = x1 - tmp[j].getLocation().x;
261 dy = y1 - tmp[j].getLocation().y;
262 if (e.isControlDown())
263 {
264 ModelMan.addCurrentObject(tmp[j].myObject);
265 }
266 else
267 {
268 ModelMan.setCurrentObject(tmp[j].myObject);
269 }
270 j = tmp.length;
271
272 }
273 ++j;
274 }
275 if (!gotIt)
276 {
277 ModelMan.resetCurrentObjects();
278 }
279 if (e.getClickCount() == 2)
280 {
281 NOToolBar2.setCurrentTool(-1);
282 EditorFactory.getObjectEdit(
283 ModelMan.getCurrentObject());
284 }
285 }
286 if (e.getButton() == MouseEvent.BUTTON1)
287 {
288
289 switch (NOToolBar2.getCurrentTool())
290 {
291
292 case NOToolBar2.TOOL_TABLE :
293 ModelMan.addEntity(
294 (ConceptualModel) myModel,
295 e.getPoint());
296
297 ModelMan.moveCurrentObjects(0, 0);
298 break;
299
300 case NOToolBar2.TOOL_LABEL :
301 ModelMan.addLabel(myModel, e.getPoint());
302
303 ModelMan.moveCurrentObjects(0, 0);
304 break;
305
306 case NOToolBar2.TOOL_ASSOCIATION :
307 ModelMan.addAssociation(
308 (ConceptualModel) myModel,
309 e.getPoint());
310
311 ModelMan.moveCurrentObjects(0, 0);
312 break;
313
314 case NOToolBar2.TOOL_ASSOCIATION_LINK :
315 ModelMan.setDraggedObject(
316 ModelMan.getCurrentObject());
317
318 ModelMan.moveCurrentObjects(0, 0);
319 break;
320
321 case NOToolBar2.TOOL_INHERITANCE :
322 ModelMan.setDraggedObject(
323 ModelMan.getCurrentObject());
324
325 ModelMan.moveCurrentObjects(0, 0);
326 break;
327
328 case NOToolBar2.TOOL_RESIZE :
329 Vector vObjects = ModelMan.getCurrentObjects();
330 if (vObjects.size() > 0)
331 {
332
333 for (int i = 0; i < vObjects.size(); i++)
334 {
335 if (((Polygon) ((BaseClass) vObjects
336 .elementAt(i))
337 .getObjectView()
338 .getSurface())
339 .contains(x1, y1))
340 {
341 ModelMan.setObjectToResize(
342 (BaseClass) vObjects.elementAt(i));
343 break;
344 }
345 }
346 }
347 else
348 {
349 ModelMan.setObjectToResize(null);
350 }
351 break;
352 }
353 }
354 if (ModelMan.getCurrentObject() == null
355 && e.getClickCount() == 2)
356 {
357 NOToolBar2.setCurrentTool(-1);
358 EditorFactory.getModelEdit();
359 }
360
361 drawingArea.repaint();
362 }
363 /***
364 * What to do when the mouse is released
365 *
366 * @param e the mouse event to handle
367 */
368 public final void mouseReleased(final MouseEvent e)
369 {
370 this.evaluatePopup(e);
371 fullRefresh = true;
372 ModelMan.resetActionLocations();
373
374 x2 = e.getX();
375 y2 = e.getY();
376 if (NOToolBar2.getCurrentTool() == NOToolBar2.TOOL_SELECTION
377 && (ModelMan.getCurrentObjects().size() > 0))
378 {
379 ModelMan.moveCurrentObjects(x2 - x1, y2 - y1);
380 }
381 else if (
382 (NOToolBar2.getCurrentTool()
383 == NOToolBar2.TOOL_ASSOCIATION_LINK
384 || NOToolBar2.getCurrentTool()
385 == NOToolBar2.TOOL_INHERITANCE)
386 && ModelMan.getDraggedObject() != null
387 && ModelMan.getDropTargetObject() != null)
388 {
389 manageDnD();
390 }
391 NOToolBar2.setCurrentTool(NOToolBar2.TOOL_SELECTION);
392 drawingArea.repaint();
393 }
394 /***
395 * Evaluate if popup menu has been requested
396 *
397 * @param e the mouse event
398 */
399 private void evaluatePopup(final MouseEvent e)
400 {
401 if (e.isPopupTrigger()
402 && (NOToolBar2.getCurrentTool() == NOToolBar2.TOOL_SELECTION))
403 {
404 MenuFactory.getModelPopupMenu().setData(e.getX(), e.getY());
405 MenuFactory.getModelPopupMenu().show(
406 drawingArea,
407 e.getX(),
408 e.getY());
409 }
410 }
411 }
412 /***
413 * MyMouseMotionListener the class which handles mouse moves on the
414 * conceptual model
415 *
416 * @author <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
417 * @created December 22, 2003
418 */
419 class MyMouseMotionListener extends MouseMotionAdapter
420 {
421 /***
422 * What to do when the mouse is dragged
423 *
424 * @param e the mouse event to handle
425 */
426 public final void mouseDragged(final MouseEvent e)
427 {
428 Graphics2D ptrGphx2D = (Graphics2D) drawingArea.getGraphics();
429 x2 = e.getX();
430 y2 = e.getY();
431
432 switch (NOToolBar2.getCurrentTool())
433 {
434
435 case NOToolBar2.TOOL_SELECTION :
436 if (ModelMan.getCurrentObjects().size() > 0)
437 {
438 if (ModelMan.getCurrentObject().getObjectView()
439 instanceof LineView)
440 {
441
442 x1 = 0;
443 y1 = 0;
444 dx = 0;
445 dy = 0;
446 }
447 ModelMan
448 .getCurrentObject()
449 .getObjectView()
450 .setActionLocation(
451 x2 - dx,
452 y2 - dy);
453 }
454 break;
455
456 case NOToolBar2.TOOL_RESIZE :
457 if (ModelMan.getObjectToResize() != null)
458 {
459
460 int curX =
461 (int) ModelMan
462 .getObjectToResize()
463 .getObjectView()
464 .getLocation()
465 .getX();
466 int curY =
467 (int) ModelMan
468 .getObjectToResize()
469 .getObjectView()
470 .getLocation()
471 .getY();
472 int curWidth =
473 (int) ModelMan
474 .getObjectToResize()
475 .getObjectView()
476 .getSize()
477 .getWidth();
478 int curHeight =
479 (int) ModelMan
480 .getObjectToResize()
481 .getObjectView()
482 .getSize()
483 .getHeight();
484
485 switch (getCursor().getType())
486 {
487 case SelectionPoint.NORTH_WEST :
488 if ((curWidth + curX - x2
489 >= CstGraphics.MIN_OBJECT_SIZE.getWidth())
490 && (curHeight + curY - y2
491 >= CstGraphics
492 .MIN_OBJECT_SIZE
493 .getHeight()))
494 {
495 ModelMan
496 .getObjectToResize()
497 .getObjectView()
498 .setLocation(
499 new Point(x2, y2));
500 ModelMan
501 .getObjectToResize()
502 .getObjectView()
503 .setSize(
504 new Dimension(
505 curWidth + curX - x2,
506 curHeight + curY - y2));
507 }
508 break;
509 case SelectionPoint.NORTH :
510 if (curHeight + curY - y2
511 >= CstGraphics
512 .MIN_OBJECT_SIZE
513 .getHeight())
514 {
515 ModelMan
516 .getObjectToResize()
517 .getObjectView()
518 .setLocation(
519 new Point(curX, y2));
520 ModelMan
521 .getObjectToResize()
522 .getObjectView()
523 .setSize(
524 new Dimension(
525 curWidth,
526 curHeight + curY - y2));
527 }
528 break;
529 case SelectionPoint.NORTH_EAST :
530 if ((x2 - curX
531 >= CstGraphics.MIN_OBJECT_SIZE.getWidth())
532 && (curHeight + curY - y2
533 >= CstGraphics
534 .MIN_OBJECT_SIZE
535 .getHeight()))
536 {
537 ModelMan
538 .getObjectToResize()
539 .getObjectView()
540 .setLocation(
541 new Point(curX, y2));
542 ModelMan
543 .getObjectToResize()
544 .getObjectView()
545 .setSize(
546 new Dimension(
547 x2 - curX,
548 curHeight + curY - y2));
549 }
550 break;
551 case SelectionPoint.WEST :
552 if (curWidth + curX - x2
553 >= CstGraphics.MIN_OBJECT_SIZE.getWidth())
554 {
555 ModelMan
556 .getObjectToResize()
557 .getObjectView()
558 .setLocation(
559 new Point(x2, curY));
560 ModelMan
561 .getObjectToResize()
562 .getObjectView()
563 .setSize(
564 new Dimension(
565 curWidth + curX - x2,
566 curHeight));
567 }
568 break;
569 case SelectionPoint.EAST :
570 if (x2 - curX
571 >= CstGraphics.MIN_OBJECT_SIZE.getWidth())
572 {
573 ModelMan
574 .getObjectToResize()
575 .getObjectView()
576 .setSize(
577 new Dimension(
578 x2 - curX,
579 curHeight));
580 }
581 break;
582 case SelectionPoint.SOUTH_WEST :
583 if ((curWidth + curX - x2
584 >= CstGraphics.MIN_OBJECT_SIZE.getWidth())
585 && (y2 - curY
586 >= CstGraphics
587 .MIN_OBJECT_SIZE
588 .getHeight()))
589 {
590 ModelMan
591 .getObjectToResize()
592 .getObjectView()
593 .setLocation(
594 new Point(x2, curY));
595 ModelMan
596 .getObjectToResize()
597 .getObjectView()
598 .setSize(
599 new Dimension(
600 curWidth + curX - x2,
601 y2 - curY));
602 }
603 break;
604 case SelectionPoint.SOUTH :
605 if (y2 - curY
606 >= CstGraphics
607 .MIN_OBJECT_SIZE
608 .getHeight())
609 {
610 ModelMan
611 .getObjectToResize()
612 .getObjectView()
613 .setSize(
614 new Dimension(curWidth, y2 - curY));
615 }
616 break;
617 case SelectionPoint.SOUTH_EAST :
618 if ((y2 - curY
619 >= CstGraphics.MIN_OBJECT_SIZE.getHeight()
620 && x2 - curX
621 >= CstGraphics
622 .MIN_OBJECT_SIZE
623 .getWidth()))
624 {
625 ModelMan
626 .getObjectToResize()
627 .getObjectView()
628 .setSize(
629 new Dimension(
630 x2 - curX,
631 y2 - curY));
632 }
633 break;
634 }
635 }
636 break;
637
638 case NOToolBar2.TOOL_INHERITANCE :
639
640
641 case NOToolBar2.TOOL_ASSOCIATION_LINK :
642
643
644 boolean targetNotFound = true;
645
646 if (ModelMan.getDraggedObject() != null)
647 {
648
649 ptrGphx2D.setColor(
650 CstGraphics.MODEL_BACKGROUND_COLOR);
651 ptrGphx2D.fillRect(
652 Math.min(x1, x2),
653 Math.min(y1, y2),
654 Math.abs(x2 - x1),
655 Math.abs(y2 - y1));
656 ptrGphx2D.setColor(Color.BLACK);
657 ptrGphx2D.drawLine(x1, y1, x2, y2);
658
659 for (int i = 0;
660 i < ((ConceptualModel) myModel).countEntities();
661 i++)
662 {
663 if (((Polygon) ((ConceptualModel) myModel)
664 .getEntityAt(i)
665 .getObjectView()
666 .getSurface())
667 .contains(
668 x2
669 + e.getComponent().getX()
670 + getViewPosition().x,
671 y2
672 + e.getComponent().getY()
673 + getViewPosition().y))
674 {
675
676
677 if (!ModelMan
678 .getDraggedObject()
679 .equals(
680 (BaseObject)
681 (
682 (
683 ConceptualModel) myModel)
684 .getEntityAt(
685 i)))
686 {
687 ModelMan.setDropTargetObject(
688 (BaseObject)
689 (
690 (
691 ConceptualModel) myModel)
692 .getEntityAt(
693 i));
694 targetNotFound = false;
695 break;
696 }
697 }
698 }
699
700
701 if (targetNotFound)
702 {
703 for (int i = 0;
704 i
705 < ((ConceptualModel) myModel)
706 .countAssociation();
707 i++)
708 {
709 if (((ConceptualModel) myModel)
710 .getAssociationAt(i)
711 .getObjectView()
712 .getSurface()
713 .contains(
714 x2
715 + e.getComponent().getX()
716 + getViewPosition().x,
717 y2
718 + e.getComponent().getY()
719 + getViewPosition().y))
720 {
721
722
723 if (!ModelMan
724 .getDraggedObject()
725 .equals(
726 (BaseObject)
727 (
728 (
729 ConceptualModel) myModel)
730 .getAssociationAt(
731 i)))
732 {
733 ModelMan.setDropTargetObject(
734 (BaseObject)
735 (
736 (
737 ConceptualModel) myModel)
738 .getAssociationAt(
739 i));
740 break;
741 }
742 }
743 }
744 }
745 }
746 break;
747 }
748
749 drawingArea.repaint();
750 }
751 /***
752 * What to do when the mouse is moved
753 *
754 * @param e the mouse event to handle
755 */
756 public final void mouseMoved(final MouseEvent e)
757 {
758 if ((NOToolBar2.getCurrentTool() == NOToolBar2.TOOL_SELECTION)
759 || (NOToolBar2.getCurrentTool() == NOToolBar2.TOOL_RESIZE))
760 {
761 boolean resizing = false;
762 int position = 4;
763
764 Vector vObjects = ModelMan.getCurrentObjects();
765 for (int i = 0; i < vObjects.size(); i++)
766 {
767 SelectionPoint[] tmp =
768 ((ObjectView) ((BaseObject) vObjects.elementAt(i))
769 .getObjectView())
770 .getSelectionPoints();
771 int j = 0;
772 while ((!resizing) && (j < tmp.length))
773 {
774
775 if (tmp[j]
776 .getSurface()
777 .contains(e.getX(), e.getY()))
778 {
779 resizing = true;
780 position = j + 4;
781 }
782 ++j;
783 }
784 }
785
786 if (resizing)
787 {
788 NOToolBar2.setCurrentTool(NOToolBar2.TOOL_RESIZE);
789 setCursor(new Cursor(position));
790 }
791 else
792 {
793 NOToolBar2.setCurrentTool(NOToolBar2.TOOL_SELECTION);
794 setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
795 }
796 }
797 }
798 }
799 }